﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WorkTaskInfoController : ControllerBase
    {
        private readonly ILogUtil _LogUtil;
        private readonly WorkTaskInfoApp _WorkTaskInfoApp;
        private readonly TaskRouteDetailRecordInfoApp _TaskRouteDetailRecordInfoApp;

        public WorkTaskInfoController(ILogUtil logUtil
            , WorkTaskInfoApp workTaskInfoApp
            , TaskRouteDetailRecordInfoApp taskRouteDetailRecordInfoApp)
        {
            _LogUtil = logUtil;
            _WorkTaskInfoApp = workTaskInfoApp;
            _TaskRouteDetailRecordInfoApp = taskRouteDetailRecordInfoApp;
        }

        [HttpPost]
        public async Task<HeaderResult<List<WCSWorkTaskInfoDto>>> GetPagedWorkTaskInfoAsync(QueryWorkTaskInfoDto queryWorkTaskInfoDto)
        {
            HeaderResult<List<WCSWorkTaskInfoDto>> headerResult = new HeaderResult<List<WCSWorkTaskInfoDto>>();
            try
            {
                PageDto<WCSWorkTaskInfoDto> pageDto = await _WorkTaskInfoApp.GetPagedWorkTaskInfoAsync(queryWorkTaskInfoDto);
                headerResult.IsSucceed = true;
                headerResult.Result = pageDto.Result;
                headerResult.Total = pageDto.Total;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpGet]
        public async Task<HeaderResult<List<TaskRouteDetailRecordInfo>>> GetTaskRouteDetailRecordInfoAsync(long taskId)
        {
            HeaderResult<List<TaskRouteDetailRecordInfo>> headerResult = new HeaderResult<List<TaskRouteDetailRecordInfo>>();

            try
            {
                var result = await _TaskRouteDetailRecordInfoApp.GetTaskRouteDetailRecordInfoAsync(taskId);
                headerResult.IsSucceed = true;
                headerResult.Result = result;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }

            return headerResult;
        }


        [HttpGet]
        public async Task<HeaderResult<RouteDetailInfo>> GetNextRouteDetailInfoByIdAsync(long Id)
        {
            HeaderResult<RouteDetailInfo> headerResult = new HeaderResult<RouteDetailInfo>();

            try
            {
                var result = await _WorkTaskInfoApp.GetNextRouteDetailInfoByIdAsync(Id);
                headerResult.IsSucceed = true;
                headerResult.Result = result;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }

            return headerResult;
        }

        [HttpGet]
        public async Task<HeaderResult<PlcWriteDbInfo>> ScGetPlcWriteDbInfoAsync(string scEquipment, string objectType)
        {
            HeaderResult<PlcWriteDbInfo> headerResult = new HeaderResult<PlcWriteDbInfo>();

            try
            {
                var result = await _WorkTaskInfoApp.ScGetPlcWriteDbInfoAsync(scEquipment, objectType);
                headerResult.IsSucceed = true;
                headerResult.Result = result;
                headerResult.Message = "查询成功";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }

            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> ScUpdateWorkTaskInfoAsync(WCSWorkTaskInfoDto wCSWorkTaskInfoDto)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _WorkTaskInfoApp.ScUpdateWorkTaskInfoAsync(wCSWorkTaskInfoDto);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "修改成功" : "修改失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> InsertTaskRouteDetailRecordInfoAsync(TaskRouteDetailRecordInfo taskRouteDetailRecordInfo)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _WorkTaskInfoApp.InsertTaskRouteDetailRecordInfoAsync(taskRouteDetailRecordInfo);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "修改成功" : "修改失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }

        [HttpPost]
        public async Task<HeaderResult<bool>> ScUpdateWorkTaskInfoRouteDetailAsync(WCSWorkTaskInfoDto wCSWorkTaskInfoDto)
        {
            HeaderResult<bool> headerResult = new HeaderResult<bool>();
            try
            {
                var result = await _WorkTaskInfoApp.ScUpdateWorkTaskInfoRouteDetailAsync(wCSWorkTaskInfoDto);
                headerResult.IsSucceed = result;
                headerResult.Result = result;
                headerResult.Message = result ? "修改成功" : "修改失败";
            }
            catch (Exception ex)
            {
                headerResult.IsSucceed = false;
                headerResult.Message = ex.ToString();
            }
            return headerResult;
        }
    }
}
